﻿//-----------------------------------------------------------------------
// <copyright file="Encoder.cs" company="iTunesEncodeNet">
//  (c) 2009 Vlasenko Dmitry
// </copyright>
//-----------------------------------------------------------------------

namespace ITunesEncodeNet
{
    using System.IO;
    using System.Threading;
    using iTunesLib;

    /// <summary>
    /// Provides methods for encoding *.wav files into *.m4a files.
    /// </summary>
    internal class Encoder
    {
        /// <summary>
        /// iTunes application.
        /// </summary>
        private iTunesApp itunes;

        /// <summary>
        /// iTunes main playlist.
        /// </summary>
        private IITLibraryPlaylist library;

        /// <summary>
        /// Initializes a new instance of the Encoder class.
        /// </summary>
        public Encoder()
        {
            this.itunes = new iTunesAppClass();
            this.library = this.itunes.LibraryPlaylist;
            this.itunes.CurrentEncoder = this.itunes.Encoders.get_ItemByName("Lossless Encoder");
        }

        /// <summary>
        /// Encodes *.wav file into *.m4a file.
        /// </summary>
        /// <param name="fileName">Full path to *.wav file.</param>
        /// <param name="artist">Track artist.</param>
        /// <param name="name">Track name.</param>
        /// <param name="album">Album title.</param>
        /// <param name="trackNumber">Track number.</param>
        /// <param name="year">Publication year.</param>
        /// <param name="genre">Track genre.</param>
        /// <param name="comment">Track comment.</param>
        /// <returns>Name of result *.m4a file.</returns>
        public string Encode(string fileName, string artist, string name, string album, uint? trackNumber, uint? year, string genre, string comment)
        {
            IITOperationStatus status;
            object track;

            IITFileOrCDTrack tempTrack;
            IITFileOrCDTrack resultTrack;

            string resultTrackFileName;

            status = this.library.AddFile(fileName);

            while (status.InProgress)
            {
                Thread.Sleep(100);
            }

            track = status.Tracks[1];
            tempTrack = (IITFileOrCDTrack)status.Tracks[1];

            status = this.itunes.ConvertTrack(ref track);

            while (status.InProgress)
            {
                Thread.Sleep(500);
            }

            resultTrack = (IITFileOrCDTrack)status.Tracks[1];

            if (!string.IsNullOrEmpty(artist))
            {
                resultTrack.Artist = artist;
            }

            if (!string.IsNullOrEmpty(name))
            {
                resultTrack.Name = name;
            }

            if (!string.IsNullOrEmpty(album))
            {
                resultTrack.Album = album;
            }

            if (trackNumber != null)
            {
                resultTrack.TrackNumber = (int)trackNumber;
            }

            if (year != null)
            {
                resultTrack.Year = (int)year;
            }

            if (!string.IsNullOrEmpty(genre))
            {
                resultTrack.Genre = genre;
            }

            if (!string.IsNullOrEmpty(comment))
            {
                resultTrack.Comment = comment;
            }

            resultTrackFileName = resultTrack.Location;

            tempTrack.Delete();
            resultTrack.Delete();

            return resultTrackFileName;
        }
    }
}
